﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using DZTC.UselessAppEver.Core;
using NUnit.Framework;

namespace DZTC.UselessAppEver.Tests
{
    [TestFixture]
    internal class OptionsTests
    {
        public class OptionsException : Exception
        {


            public OptionsException(string message)
                : base(message)
            {
            }

        }

        private void ErrorCallback(string str)
        {
            throw new OptionsException(str);
        }

        [Test]
        public void TestNoOptions()
        {
            var options = ApplicationOptions.Parse(new string[0], ErrorCallback);

            Assert.AreEqual(options.Mode, ApplicationOptions.ApplicationMode.Default);
            Assert.IsNull(options.FileName);
        }

        [Test]
        public void TestInstallMode()
        {
            var options = ApplicationOptions.Parse(new[] {"--install"}, ErrorCallback);

            Assert.AreEqual(options.Mode, ApplicationOptions.ApplicationMode.Install);
            Assert.IsNull(options.FileName);
        }

        [Test]
        public void TestEncode()
        {
            const string fname = @"D:\files\lol.exe";
            var options = ApplicationOptions.Parse(new[] {"--encode", fname}, ErrorCallback);

            Assert.AreEqual(options.Mode, ApplicationOptions.ApplicationMode.Encode);
            Assert.AreEqual(options.FileName, fname);
        }

        [Test]
        public void TestDecode()
        {
            const string fname = @"D:\files\lol.exe";
            var options = ApplicationOptions.Parse(new[] { "--decode", fname }, ErrorCallback);

            Assert.AreEqual(options.Mode, ApplicationOptions.ApplicationMode.Decode);
            Assert.AreEqual(options.FileName, fname);
        }

        [Test]
        [ExpectedException(typeof (OptionsException))]
        public void TestWrongOption()
        {
            Assert.IsNull(ApplicationOptions.Parse(new[] {"--foobar"}, _ => { }));
            ApplicationOptions.Parse(new[] {"--foobar"}, ErrorCallback);
        }

        [Test]
        [ExpectedException(typeof(OptionsException))]
        public void TestNoFileNameForEncoding()
        {
            Assert.IsNull(ApplicationOptions.Parse(new[] { "--encode" }, _ => { }));
            ApplicationOptions.Parse(new[] { "--encode" }, ErrorCallback);
        }

        [Test]
        [ExpectedException(typeof(OptionsException))]
        public void TestNoFileNameForDecoding()
        {
            Assert.IsNull(ApplicationOptions.Parse(new[] { "--decode" }, _ => { }));
            ApplicationOptions.Parse(new[] { "--decode" }, ErrorCallback);
        }
    }
}
